Conditions | 1 |
Paths | > 20000 |
Total Lines | 378 |
Code Lines | 262 |
Lines | 0 |
Ratio | 0 % |
Changes | 17 | ||
Bugs | 2 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var strAsc = String.fromCharCode(9650); //▲ |
||
6 | var table = new function () { |
||
7 | this.rq = null; |
||
8 | this.tail = []; |
||
9 | |||
10 | this.ReloadData = function (tableId) { |
||
11 | var request = {}; |
||
12 | this.BuildRequest(request, tableId); |
||
13 | this.LoadData(tableId, request); |
||
14 | }; |
||
15 | |||
16 | this.BuildRequest = function (request, crntTableId, skipPropertyArray) { |
||
17 | this.rq = request; |
||
18 | this.checkSkip = function (skipProperty) { |
||
19 | var result = false; |
||
20 | if (skipPropertyArray && Object.prototype |
||
21 | .toString.call(skipPropertyArray) === '[object Array]') { |
||
22 | if (skipPropertyArray.indexOf(skipProperty) >= 0) { |
||
23 | result = true; |
||
24 | } |
||
25 | } |
||
26 | return result; |
||
27 | }; |
||
28 | this.getSort = function () { |
||
29 | var table = document.getElementById(crntTableId); |
||
30 | var thTags = table.getElementsByTagName("thead")[0] |
||
31 | .getElementsByTagName("th"); |
||
32 | for (var i = 0; i < thTags.length; i++) { |
||
33 | if (thTags[i].getElementsByTagName("a")[0] && thTags[i] |
||
34 | .getElementsByTagName("a")[0].getElementsByTagName("span") |
||
35 | .length === 1 |
||
36 | ) { |
||
37 | var order = thTags[i].getElementsByTagName("a")[0] |
||
38 | .getElementsByTagName("span")[0].innerHTML; |
||
39 | if (order.length === 1) { |
||
40 | this.rq.colNo = i; |
||
41 | this.rq.colOrd = (order === window.strDesc) ? |
||
42 | "desc" : "asc"; |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | }; |
||
47 | this.getFilter = function () { |
||
48 | var r = this.getFilterFieldsByTbaleID(crntTableId); |
||
49 | if (r.filter !== null) { |
||
50 | this.rq.filter = r.filter; |
||
51 | } |
||
52 | if (r.filterBy !== null) { |
||
53 | this.rq.filterBy = r.filterBy; |
||
54 | } |
||
55 | }; |
||
56 | |||
57 | /* Build request object */ |
||
58 | if (!this.checkSkip("sort")) { |
||
59 | this.getSort(); |
||
60 | } |
||
61 | if (!this.checkSkip("filter")) { |
||
62 | this.getFilter(); |
||
63 | } |
||
64 | |||
65 | this.rq.tableId = crntTableId; |
||
66 | return this.rq; |
||
67 | }; |
||
68 | |||
69 | this.RequestToUrl = function (rq) { |
||
70 | var url = location.pathname + ".json" + location.search; |
||
71 | if (typeof rq === "object") { |
||
72 | var getUrlVarName = { |
||
73 | colNo: "col", colOrd: "ord", filter: "filter", |
||
74 | filterBy: "filter-by", pageNo: "pg", exportType: "export", |
||
75 | tableId: "table-id" |
||
76 | }; |
||
77 | var flagFirst = location.search.length < 1 ? true : false; |
||
78 | var length = rq.length; |
||
79 | for (var i = 0; i < length.length; i++) { |
||
80 | var clue = flagFirst === true ? "?" : "&"; |
||
81 | url += clue + getUrlVarName[i] + "=" + rq[i]; |
||
82 | flagFirst = false; |
||
83 | } |
||
84 | } |
||
85 | return url; |
||
86 | }; |
||
87 | |||
88 | this.Filter = function (field) { |
||
89 | var crntTableId = this.FilterGetTableId(field); |
||
90 | if (crntTableId !== null) { |
||
91 | var request = {}; |
||
92 | var exRq = this.rq; |
||
93 | this.BuildRequest(request, crntTableId); |
||
94 | if (exRq === null || |
||
95 | request.filter !== exRq.filter || |
||
96 | request.filterBy !== exRq.filterBy |
||
97 | ) { |
||
98 | this.LoadData(crntTableId, request); |
||
99 | } |
||
100 | } |
||
101 | }; |
||
102 | |||
103 | this.FilterGetTableId = function (field) { |
||
104 | if (field.tagName.toLowerCase() !== "select") { |
||
105 | return field.getAttribute("data-table-id"); |
||
106 | } else { |
||
|
|||
107 | var f = field.parentNode.parentNode.getElementsByTagName("input")[0]; |
||
108 | return '' === f.value ? null : f.getAttribute("data-table-id"); |
||
109 | } |
||
110 | }; |
||
111 | |||
112 | this.GoPage = function (lnk) { |
||
113 | var request = {}; |
||
114 | var table = this.getParent(lnk, "table"); |
||
115 | var crntTableId = table.getAttribute("id"); |
||
116 | this.BuildRequest(request, crntTableId); |
||
117 | //check & serve pagination jump links |
||
118 | var jumpDir = lnk.innerHTML.trim().substr(0, 1); |
||
119 | if (jumpDir === "+" || jumpDir === "-") { |
||
120 | var current = table.querySelector("tfoot .paging .a").innerHTML; |
||
121 | var jump = lnk.innerHTML.replace("K", "000").replace("M", "000000000"); |
||
122 | var jumpPage = (parseInt(current) + parseInt(jump)); |
||
123 | lnk.parentNode.setAttribute("data-page", jumpPage); |
||
124 | lnk.style.transform = "none"; |
||
125 | } |
||
126 | request.pageNo = lnk.parentNode.hasAttribute("data-page") ? |
||
127 | lnk.parentNode.getAttribute("data-page") : |
||
128 | lnk.innerHTML; |
||
129 | this.LoadData(crntTableId, request); |
||
130 | return false; |
||
131 | }; |
||
132 | |||
133 | this.Export = function (lnk, eType) { |
||
134 | var request = {}; |
||
135 | var crntTableId = this.getParent(lnk, "table").getAttribute("id"); |
||
136 | this.BuildRequest(request, crntTableId); |
||
137 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? eType : "csv"; |
||
138 | window.open(this.RequestToUrl(request)); |
||
139 | return false; |
||
140 | }; |
||
141 | |||
142 | this.Sort = function (colNo, lnk) { |
||
143 | var request = {}; |
||
144 | var crntTableId = this.getParent(lnk, "table").getAttribute("id"); |
||
145 | this.BuildRequest(request, crntTableId); |
||
146 | if (Math.round(colNo) === request.colNo) { |
||
147 | request.colOrd = request.colOrd === "asc" ? "desc" : "asc"; |
||
148 | } else { |
||
149 | request.colNo = Math.round(colNo); |
||
150 | request.colOrd = "asc"; |
||
151 | } |
||
152 | this.LoadData(crntTableId, request); |
||
153 | /* Clear and add new sort arrow */ |
||
154 | var headSpans = this.getParent(lnk, "thead").getElementsByTagName("span"); |
||
155 | var length = headSpans.length; |
||
156 | for (var i = 0; i < length; i++) { |
||
157 | headSpans[i].innerHTML = ""; |
||
158 | } |
||
159 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? window.strDesc : window.strAsc); |
||
160 | }; |
||
161 | |||
162 | this.DrawSection = function (tableContainer, dt, tSection) { |
||
163 | var section = tSection === "tfoot" ? "tfoot" : "tbody"; |
||
164 | tSection = document.getElementById(tableContainer). |
||
165 | getElementsByTagName(section)[0]; |
||
166 | this.clearSection(tSection); |
||
167 | for (var i = 0; i < dt.length; i++) { |
||
168 | var row = dt[i]; |
||
169 | var tRow = document.createElement("tr"); |
||
170 | |||
171 | this.DrawRow(row, tRow); |
||
172 | |||
173 | tSection.appendChild(tRow); |
||
174 | if (section === "tfoot") { |
||
175 | this.footerProcessPaginationLinks(tSection); |
||
176 | } |
||
177 | this.AppendRowCalback(tableContainer); |
||
178 | } |
||
179 | }; |
||
180 | |||
181 | this.DrawRow = function (row, tRow) { |
||
182 | var length = row.length; |
||
183 | for (var i = 0; i < length; i++) { |
||
184 | var tCell = document.createElement("td"); |
||
185 | if (typeof row[i] === "string" || |
||
186 | typeof row[i] === "number" |
||
187 | ) { |
||
188 | tCell.innerHTML = row[i]; |
||
189 | } else if (typeof row[i] === "object") { |
||
190 | this.DrawCellFromObject(row, row[i], tCell); |
||
191 | } |
||
192 | tRow.appendChild(tCell); |
||
193 | } |
||
194 | }; |
||
195 | |||
196 | this.DrawCellFromObject = function (row, cell, tCell) { |
||
197 | var length = row[cell].length; |
||
198 | for (var i = 0; i < length; i++) { |
||
199 | if (typeof row[cell][i] === "string") { |
||
200 | tCell.innerHTML = row[cell][i]; |
||
201 | } else if (typeof row[cell][i] === "object") { |
||
202 | var length2 = row[cell][i].length; |
||
203 | for (var j = 0; j < length2; j++) { |
||
204 | tCell.setAttribute(j, row[cell][i][j]); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | }; |
||
209 | |||
210 | this.footerProcessPaginationLinks = function (tSection) { |
||
211 | var pLinks = tSection.querySelectorAll(".paging a"); |
||
212 | if (pLinks.length > 0) { |
||
213 | for (var j = 0; j < pLinks.length; j++) { |
||
214 | pLinks[j].setAttribute("href", "javascript:void(0);"); |
||
215 | pLinks[j].setAttribute("onclick", "return table.GoPage(this);"); |
||
216 | } |
||
217 | } |
||
218 | }; |
||
219 | |||
220 | this.clearSection = function (tSection) { |
||
221 | if (this.iePrior(9)) { |
||
222 | if (tSection.firstChild) { |
||
223 | while (tSection.firstChild) { |
||
224 | tSection.removeChild(tSection.firstChild); |
||
225 | } |
||
226 | } |
||
227 | } else { |
||
228 | tSection.innerHTML = ""; |
||
229 | } |
||
230 | }; |
||
231 | |||
232 | this.SetTheTableColumnsHoverEffect = function (tableContainer) { |
||
233 | if (this.iePrior(9)) { |
||
234 | return; |
||
235 | } |
||
236 | var tContainer = document.getElementById(tableContainer); |
||
237 | var tHcells = tContainer.rows[0].cells; |
||
238 | for (var i = 0; i < tHcells.length; i++) { |
||
239 | if (tHcells[i].firstChild.tagName === "A") { |
||
240 | tHcells[i].firstChild.setAttribute("onmouseover", "table.ColumnHover('" + tableContainer + "'," + i + ");"); |
||
241 | tHcells[i].firstChild.setAttribute("onmouseout", "table.ColumnHover('" + tableContainer + "');"); |
||
242 | } |
||
243 | } |
||
244 | var pLinks = tContainer.querySelectorAll("tfoot .paging a"); |
||
245 | if (pLinks.length > 0) { |
||
246 | for (var j = 0; j < pLinks.length; j++) { |
||
247 | pLinks[j].setAttribute("href", "javascript:void(0);"); |
||
248 | pLinks[j].setAttribute("onclick", "return table.GoPage(this);"); |
||
249 | } |
||
250 | } |
||
251 | }; |
||
252 | |||
253 | this.ColumnHover = function (tableContainer, index) { |
||
254 | if (this.iePrior(9)) { |
||
255 | return; |
||
256 | } |
||
257 | var tRow = document.getElementById(tableContainer).rows; |
||
258 | index = Math.round(index); |
||
259 | for (var i = 0; i < (tRow.length - 1); i++) { |
||
260 | if (index >= 0) { |
||
261 | tRow[i].cells[index].setAttribute("lang", "col-hover"); |
||
262 | } else { |
||
263 | for (var j = 0; j < tRow[i].cells.length; j++) { |
||
264 | if (tRow[i].cells[j].lang) { |
||
265 | tRow[i].cells[j].removeAttribute("lang"); |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | }; |
||
271 | |||
272 | this.getFilterFieldsByTbaleID = function (tableID) { |
||
273 | var fields = {filterBy: null, filter: null}; |
||
274 | var filterDiv = this.getFilterDivByTableIDOrNull(tableID); |
||
275 | if (filterDiv !== null) { |
||
276 | var selectObj = filterDiv.getElementsByTagName("select")[0]; |
||
277 | var textObj = filterDiv.getElementsByTagName("input")[0]; |
||
278 | fields.filterBy = (selectObj === null || selectObj.options[selectObj.selectedIndex].value === "all") ? null : selectObj.options[selectObj.selectedIndex].value; |
||
279 | fields.filter = (textObj === null || textObj.value.length === 0) ? null : encodeURIComponent(textObj.value.trim()); |
||
280 | } |
||
281 | return fields; |
||
282 | }; |
||
283 | |||
284 | this.getFilterDivByTableIDOrNull = function (tableID) { |
||
285 | var res = null; |
||
286 | if (document.getElementById(tableID).parentNode.getElementsByTagName("div").length > 0) { |
||
287 | for (var i = 0; i < document.getElementById(tableID).parentNode.getElementsByTagName("div").length; i++) { |
||
288 | if (document.getElementById(tableID).parentNode.getElementsByTagName("div")[i].getAttribute("class") === "filter") { |
||
289 | return document.getElementById(tableID).parentNode.getElementsByTagName("div")[i]; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | } |
||
294 | return res; |
||
295 | }; |
||
296 | |||
297 | this.LoadData = function (tableContainer, rq) { |
||
298 | this.setVisability(tableContainer, false); |
||
299 | if (window.XMLHttpRequest) { |
||
300 | xmlhttp = new XMLHttpRequest();/* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
301 | } else { /** global: ActiveXObject */ |
||
302 | xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
303 | } |
||
304 | for (var i = 0; i < this.tail.length; i++) { |
||
305 | var ex_xmlhttp = this.tail.shift(); |
||
306 | ex_xmlhttp.abort(); |
||
307 | } |
||
308 | xmlhttp.onreadystatechange = function () { |
||
309 | if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { |
||
310 | d = JSON.parse(xmlhttp.responseText); |
||
311 | table.DrawSection(tableContainer, d.body); |
||
312 | table.DrawSection(tableContainer, d.footer, "tfoot"); |
||
313 | table.LoadEndCalback(tableContainer); |
||
314 | table.setVisability(tableContainer, true); |
||
315 | if (typeof rq === "object") { |
||
316 | table.ColumnHover(tableContainer, rq.colNo); |
||
317 | } |
||
318 | } |
||
319 | }; |
||
320 | xmlhttp.open("GET", this.RequestToUrl(rq), true); |
||
321 | xmlhttp.send(); |
||
322 | this.tail.push(xmlhttp); //put in tail to may later abort any previous |
||
323 | }; |
||
324 | |||
325 | this.setVisability = function (tableContainer, rq) { |
||
326 | var tbl = document.getElementById(tableContainer); |
||
327 | if (rq === true) { |
||
328 | tbl.style.filter = "none"; |
||
329 | tbl.style.opacity = "1"; |
||
330 | tbl.style.cursor = "auto"; |
||
331 | } else if (rq === false) { |
||
332 | tbl.style.filter = "blur(1px)"; |
||
333 | tbl.style.opacity = "0.8"; |
||
334 | tbl.style.cursor = "wait"; |
||
335 | } else { |
||
336 | console.log("table error in the rq value"); /*Shows error*/ |
||
337 | } |
||
338 | }; |
||
339 | |||
340 | this.getParent = function (obj, objType) { |
||
341 | while (obj && obj.tagName !== objType.toUpperCase()) { |
||
342 | obj = obj.parentNode; |
||
343 | } |
||
344 | return obj; |
||
345 | }; |
||
346 | |||
347 | this.init = function (tableId) { |
||
348 | this.SetTheTableColumnsHoverEffect(tableId); |
||
349 | }; |
||
350 | |||
351 | this.iePrior = function (v) { |
||
352 | var rv = false; |
||
353 | if (/** global: navigator */ navigator.appName === 'Microsoft Internet Explorer') { |
||
354 | var ua = navigator.userAgent; |
||
355 | var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); |
||
356 | if (re.exec(ua) !== null) { |
||
357 | rv = parseFloat(RegExp.$1); |
||
358 | } |
||
359 | rv = rv < v ? true : false; |
||
360 | } |
||
361 | return rv; |
||
362 | }; |
||
363 | this.loadJS = function (src) { |
||
364 | var s = document.createElement('script'); |
||
365 | s.src = src; |
||
366 | document.getElementsByTagName('head')[0].appendChild(s); |
||
367 | }; |
||
368 | this.loadCSS = function (src) { |
||
369 | var s = document.createElement('link'); |
||
370 | s.href = src; |
||
371 | s.rel = "stylesheet"; |
||
372 | document.getElementsByTagName('head')[0].appendChild(s); |
||
373 | }; |
||
374 | |||
375 | this.LoadEndCalback = function (tableId) { |
||
376 | if (tableId) {/*Allows override*/ |
||
377 | } |
||
378 | }; |
||
379 | this.AppendRowCalback = function (tableId) { |
||
380 | if (tableId) {/*Allows override*/ |
||
381 | } |
||
382 | }; |
||
383 | }; |
||
384 | |||
441 | /*if(window.addEventListener){window.addEventListener('load',tablesLoadData,false);} else if(window.attachEvent){window.attachEvent('onload',tablesLoadData);}*/ |